package com.cskaoyan.service;

import com.cskaoyan.bean.BO.GoodsBO;
import com.cskaoyan.bean.BO.GoodsBO;
import com.cskaoyan.bean.PO.*;
import com.cskaoyan.bean.VO.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketGoodsAttributeMapper;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketGoodsProductMapper;
import com.cskaoyan.mapper.MarketGoodsSpecificationMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author linhanxin
 * @date 2022/8/13.
 */
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Autowired
    MarketGoodsAttributeMapper marketGoodsAttributeMapper;

    @Autowired
    MarketGoodsProductMapper marketGoodsProductMapper;

    @Autowired
    MarketGoodsSpecificationMapper marketGoodsSpecificationMapper;

    @Override
    public CommonData<MarketGoodsVo> query(BasePageInfo info, String goodsSn, Integer id, String name) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketGoodsExample example = new MarketGoodsExample();
        String orderByClause = info.getSort() + " " + info.getOrder();
        example.setOrderByClause(orderByClause);
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        if (goodsSn != null) {
            goodsSn = "%" + goodsSn + "%";
            criteria.andGoodsSnLike(goodsSn);
        }
        if (id != null) {
            criteria.andIdEqualTo(id);
        }
        if (name != null) {
            name = "%" + name + "%";
            criteria.andNameLike(name);
        }
        List<MarketGoodsVo> goodsList = marketGoodsMapper.selectByExampleVo(example);
        PageInfo<MarketGoodsVo> pageInfo = new PageInfo<>(goodsList);
        return CommonData.data(pageInfo);
    }

    @Override
    public void delete(Integer id) {
        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(id);
        marketGoodsMapper.deleteByExample(example);
    }

    @Override
    public void createGoods(@RequestBody GoodsBO goodsBO) {
        marketGoodsMapper.insertSelective(goodsBO.getGoods());
        for (MarketGoodsAttribute attribute : goodsBO.getAttributes()) {
            marketGoodsAttributeMapper.insertSelective(attribute);
        }
        for (MarketGoodsSpecification specification : goodsBO.getSpecifications()) {
            marketGoodsSpecificationMapper.insertSelective(specification);
        }
        for (MarketGoodsProductVo product : goodsBO.getProducts()) {
            product.setId(null);
            marketGoodsProductMapper.insertSelective(product);
        }

    }

    @Override
    public void updateGoods(@RequestBody GoodsBO goodsBO) {
        List<MarketGoodsAttribute> attributes = goodsBO.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            marketGoodsAttributeMapper.updateByPrimaryKeySelective(attribute);
        }
        marketGoodsMapper.updateByPrimaryKeySelective(goodsBO.getGoods());
        List<MarketGoodsProductVo> products = goodsBO.getProducts();
        for (MarketGoodsProductVo product : products) {
            marketGoodsProductMapper.updateByPrimaryKeySelective(product);
        }
        List<MarketGoodsSpecification> specifications = goodsBO.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            marketGoodsSpecificationMapper.updateByPrimaryKeySelective(specification);
        }
    }

    @Override
    public GoodsDetailVo detail(Integer id) {
        GoodsCategoryIdVo categoryIdVo = marketGoodsMapper.selectCategoryId(id);
        ArrayList<Integer> categoryIds = new ArrayList<>();
        categoryIds.add(categoryIdVo.getPid());
        categoryIds.add(categoryIdVo.getId());
        MarketGoodsVo goods = marketGoodsMapper.selectGoodsById(id);
        MarketGoodsAttributeExample example = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria = example.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> attributes = marketGoodsAttributeMapper.selectByExample(example);

        MarketGoodsSpecificationExample example2 = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria2 = example2.createCriteria();
        criteria2.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> specifications = marketGoodsSpecificationMapper.selectByExample(example2);

        MarketGoodsProductExample example3 = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria3 = example3.createCriteria();
        criteria3.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> products = marketGoodsProductMapper.selectByExample(example3);

       GoodsDetailVo goodsDetailVo = new GoodsDetailVo(categoryIds, goods, attributes, specifications, products);
       return goodsDetailVo;
    }

    @Override
    public GoodsCatAndBrandVo catAndBrand() {
        List<BrandVo> brandVoList = marketGoodsMapper.selectBrand();
        ArrayList<CategoryVo> categoryVoList = new ArrayList<>();
        List<CategoryVo> categoryList = marketGoodsMapper.selectCategory();
        for (CategoryVo categoryVo : categoryList) {
            //遍历获取该类目的子类目列表
            List<ChildrenEntityVo> childrenEntityVoList = marketGoodsMapper.selectChildrenCategory(categoryVo.getValue());
            CategoryVo category = new CategoryVo(categoryVo.getValue(), categoryVo.getLabel(), childrenEntityVoList);
            categoryVoList.add(category);
        }
        GoodsCatAndBrandVo goodsCatAndBrandVo = new GoodsCatAndBrandVo(categoryVoList, brandVoList);
        return goodsCatAndBrandVo;
    }
}
